package com.tz.fruit.service.impl;

import com.tz.fruit.dao.IUserDao;

import com.tz.fruit.entity.PageBean;

import com.tz.fruit.entity.User;
import com.tz.fruit.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;


/**
 * Created by Administrator on 2017/7/20.
 */
@Repository
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserDao userDao;


    @Override
    public void save(User u) {
       userDao.save(u);
    }

    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Override
    public void updateStatus(String username) {
        userDao.updateStatus(username);
    }


    @Override
    public PageBean<User> findByPage(String username, Long pageNow, Long pageSize) {
     //1.创建一个PageBean<User>对象
        PageBean<User> page = new PageBean<>();
        //2.封装满足条件需要的users集合
        List<User> users=userDao.findAll(username,pageNow,pageSize);
        page.setList(users);
        //3.封装每页显示多少条.
        page.setPageSize(pageSize);
        //4.封装当前页
        page.setPageNow(pageNow);
        //5.处理总的页数...
        //6.必须要得到总的条数.
        //从性能上面考虑,不推荐调用集合的size方法.
        long rowCount=userDao.findRowCount(username);
        Long pageCount = rowCount/pageSize;
        if(rowCount%pageSize!=0)
            pageCount++;
        page.setPageCount(pageCount);
        return page;
    }

    @Override
    public void update(User user) {
        userDao.update(user);
    }

}
